<template>
  <div class="withdraw-audit">
    <!-- 搜索栏 -->
    <div class="search-block">
      <div class="left-group">
        <el-button class="search-btn" @click="operate('add')" size="medium" type="primary">新增</el-button>
      </div>
    </div>
    <!-- 数据表 -->
    <div class="data-table">
      <el-table
          v-loading="loading"
          element-loading-text="拼命加载中..."
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(211, 215, 212, 0.5)"
          :data="tableData"
          stripe
          header-cell-class-name="headerStyle"
          style="width: 100%"
      >
        <el-table-column align="center" prop="platform_id" label="平台id"></el-table-column>
        <el-table-column align="center" prop="title" label="快讯标题"></el-table-column>
        <el-table-column align="center" prop="top" label="是否置顶">
          <template slot-scope="scope">
            <span>{{scope.row.top| newsTop}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="highlight" label="是否高亮">
          <template slot-scope="scope">
            <span>{{scope.row.highlight| newsHightlight}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="inviter" label="操作" width="150">
          <template slot-scope="item">
            <el-button type="text" @click="operate('view',item.row)" class="edit-btn">查看
            </el-button>
            <el-button type="text" @click="operate('edit',item.row)" class="edit-btn">编辑
            </el-button>
            <el-button type="text" @click="del(item.row)" class="cancel-btn">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pages
          :pages="searchForm"
          :total="total"
          @pageSizeChange="pageSizeChange"
          @pageChange="pageChange"
      ></pages>
      <!--  表单弹框-->
      <transition name="fade">
        <newsDialog
            v-if="showDialog"
            @cancel="showDialog=false"
            @success="showDialog=false;search()"
            :operateData="operateData"
            :action="action"
        ></newsDialog>
      </transition>
    </div>
  </div>
</template>

<script>
  import Pages from "@/components/PageBox.vue";
  import {getNewsList, removeNews} from "./news.service"
  import newsDialog from "@/components/NewsManage/newsDialog"

  export default {
    components: {
      Pages,
      newsDialog
    },
    data() {
      return {
        loading: false,
        showDialog: false,
        action: "",
        operateData: {},
        searchForm: {
          page: 1,
          size: 10
        },
        tableData: [],
        total: 10
      };
    },
    mounted() {
      this.init()
    },
    methods: {
      async init() {
        this.search()
      },
      async search() {
        this.loading = true;
        const res = await getNewsList(this.searchForm);
        this.tableData = res.data ? res.data.items : [];
        this.total = res.data ? Number(res.data.total) : 0;
        this.loading = false;
      },
      async operate(action, row) {
        this.showDialog = true;
        this.action = action;
        this.operateData = row;
      },
      del(row) {
        this.$confirm(
          `是否删除?`,
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
            dangerouslyUseHTMLString: true
          }
        )
          .then(() => {
            removeNews(row.id).then(res => {
              if (res.code === 0)
                this.$notify.success({
                  title: "提示",
                  message: "删除成功",
                  duration: 1500
                });
              this.search();
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除"
            });
          });
      },
      pageChange(val) {
        this.searchForm.page = val;
        this.search(val);
      },
      pageSizeChange(val) {
        this.searchForm.size = val;
        this.searchForm.page = 1;
        this.search();
      }
    }
  };
</script>

<style lang="scss" scoped>
  .withdraw-audit {
    .search-block {
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 100%;
      padding: 25px 30px;
      border-radius: 5px;
      background: #fff;
      box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;

      .item-msg {
        display: inline-block;
        margin-right: 20px;
        height: 60px;
        line-height: 60px;
        font-size: 1 size= "medium" 4;
        color: $admin_font_param;

        label {
          display: inline-block;
          min-width: 50px;
          text-align: right;
          margin-right: 10px;
        }

        .el-input {
          width: 150px;
        }

        .el-select {
          width: 150px;
        }

        .el-date-editor {
          width: 270px;
        }
      }

      .search-btn {
        margin-left: 30px;
      }

      .search-btn:nth-of-type(1) {
        margin-left: 50px;
      }

      .left-group {

      }

      .right-group {
        display: flex;

        span {
          margin-right: 10px;
        }
      }
    }

    .data-table {
      padding-bottom: 20px;
      background: #fff;
      border-radius: 5px;
      margin-top: 30px;
      box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;
    }

    .cancel-btn {
      color: red;
    }
  }
</style>
